﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Text;
using ShopModel;
using System.Data;
using System.Data.SqlClient;

namespace ShopDAO
{
    public class ViewDAO
    {
        private string connectionString;

        private SqlConnection Connection()
        {
            return new SqlConnection(connectionString);
        }

        public ViewDAO()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csShop"].ConnectionString;
        }

        /// <summary>
        /// Pobiera informacje o wybranym widoku.
        /// (Gets information about the selected view.)
        /// </summary>
        /// <param name="ViewID">
        /// ID widoku.
        /// (View ID.)
        /// </param>
        /// <param name="LanguageID">
        /// ID języka lub wartość 'null'.
        /// (Language ID or 'null' value.)
        /// </param>
        /// <returns>
        /// Zwraca obiekt View zawierający informacje o wybranym widoku.
        /// (Returns the View object that contains information about particular view.)
        /// </returns>
        public View GetViewByID(int ViewID, int? LanguageID)
        {
            SqlConnection sqlC = Connection();
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;
            View result = null;

            command.CommandText = "GetViews";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@ViewID", ViewID));
            command.Parameters.Add(new SqlParameter("@LanguageID", LanguageID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                if (sqlR.Read())
                {
                    result = new View((int)sqlR["IdView"], (string)sqlR["Title"], (string)sqlR["Template"]);
                }

                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            { 
                sqlC.Close(); 
            }

            return result;
        }

        /// <summary>
        /// Zapisuje stworzony widok do bazy.
        /// (Saves created view to the database.)
        /// </summary>
        /// <param name="ViewToSave">
        /// Obiekt View zawierający informacje o nowym widoku.
        /// (View object contains information about the new view.)
        /// </param>
        /// <param name="LanguageID">
        /// ID języka lub wartość 'null'.
        /// (Language ID or 'null' value.)
        /// </param>
        /// <returns>
        /// Zwraca obiekt Int. Jeśli zwrócona wartość jest równa '-1' wystąpił błąd z zapisem widoku.
        /// (Returns Int object. If returned  value is equal '-1', error with saving the view has occured.)
        /// </returns>
        public int SaveView(View ViewToSave, int? LanguageID)
        {
            SqlConnection sqlC = Connection();
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;

            int result = -1;

            command.CommandText = "SaveView";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@ViewID", ViewToSave.ViewID));
            command.Parameters.Add(new SqlParameter("@Title", ViewToSave.Title));
            command.Parameters.Add(new SqlParameter("@Template", ViewToSave.Template));
            command.Parameters.Add(new SqlParameter("@LanguageID", LanguageID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                if(sqlR.Read())
                result = (int)sqlR["ViewID"];

                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            { 
                sqlC.Close(); 
            }

            return result;
        }

        /// <summary>
        /// Usuwa wybrany widok z bazy. 
        /// (Removes selected view from the database.)
        /// </summary>
        /// <param name="ViewID">
        /// ID widoku.
        /// (View ID.)
        /// </param>
        /// <returns>
        /// Zwraca 'true' jeśli usuwanie danych z bazy zakończyło się sukcesem.
        /// (Returns 'true' if the removal of data from the database succeeded.)
        /// </returns>
        public bool DeleteView(int ViewID)
        {
            SqlConnection sqlC = Connection();
            SqlCommand command = sqlC.CreateCommand();
            bool result = false;

            command.CommandText = "DeleteView";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@ViewID", ViewID));

            try
            {
                sqlC.Open();
                int rows = command.ExecuteNonQuery();

                if (rows > 0)
                    result = true;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            { 
                sqlC.Close(); 
            }

            return result;
        }

        /// <summary>
        /// Pobiera listę wszystkich widoków.
        /// (Gets all views list.)
        /// </summary>
        /// <param name="LanguageID">
        /// ID języka lub wartość 'null'.
        /// (Language ID or 'null' value.)
        /// </param>
        /// <returns>
        /// Zwraca listę obiektów View zawierającą informacje na temat widoków.
        /// (Returns list of View objects containing information about views.)
        /// </returns>
        public List<View> GetViews(int? LanguageID)
        {
            SqlConnection sqlC = Connection();
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;
            List<View> result = new List<View>();

            command.CommandText = "GetViews";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@LanguageID", LanguageID));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                while(sqlR.Read())
                {
                    result.Add(new View((int)sqlR["IdView"], (string)sqlR["Title"], (string)sqlR["Template"]));
                }

                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            { 
                sqlC.Close(); 
            }

            return result;
        }
    }
}
